<?php

namespace beObject\MVC;

use beObject\Primitive\String;
use beObject\Primitive\Integer;
use beObject\Exceptions\ValidationException;

/**
 * This abstract class simplify generic input validation.
 * @author Dany Mottier
 * @copyright MD Consult
 * @license http://www.gnu.org/licenses/gpl.html
 * @link http://www.beobject-framework.net/beobject/mvc/validatorhelper/validatorhelper-class/<br />
 * @package beObject\MVC
 * @version 3.0
 */
abstract class ValidatorHelper {

  /**
   * Validates an email address.
   * @param beObject\Exceptions\ValidationException $ve The exception to update.
   * @param beObject\Primitive\String $email The email.
   * @param beObject\Primitive\String $field The field name.
   * @link http://www.beobject-framework.net/beobject/mvc/validatorhelper/validatorhelper-validateemail/
   */
  public static function validateEmail(ValidationException &$ve, String $email,
                                       String $field) {
    if (preg_match("#^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,6}$#", $email()) == 0) {
      $ve->addErrorMessage($field, new String("[email-format]"));
    }
  }

  /**
   * Validates an url that can meet the following protocols :
   * <ul>
   * <li>HTTP(S)</li>
   * <li>FTP(S)</li>
   * </ul>
   * @param \beObject\Exceptions\ValidationException $ve The exception tu update.
   * @param beObject\Primitive\String $url The url.
   * @param beObject\Primitive\String $field The field name.
   * @link http://www.beobject-framework.net/beobject/mvc/validatorhelper/validatorhelper-validateurl/
   */
  public static function validateUrl(ValidationException &$ve, String $url,
                                     String $field) {
    if (preg_match("#^(ftps?|https?)://www.[\w.-]+\.[a-zA-Z]{2,6}$#", $url()) == 0) {
      $ve->addErrorMessage($field, new String("[url-format]"));
    }
  }

  /**
   * Validates a structured communication like "001/1234/98765".
   * @param ValidationException $ve The exception to update.
   * @param IString $vcs The communication
   * @link http://be2.php.net/manual/en/function.preg-match.php
   */
//  public static function validateVcs(ValidationException &$ve, IString $vcs) {
//    if (preg_match("#^[0-9]{3}/[0-9]{4}/[0-9]{5}$#", $vcs->toString()) == 0) {
//      $ve->addErrorMessage(self::VCS, "The vcs is not in a correct format.");
//    }
//  }

  /**
   * Validates a dossier number.
   * @param ValidationException $ve The exception to update.
   * @param IString $dossier The dossier.
   * @link http://be2.php.net/manual/en/function.preg-match.php
   */
//  public static function validateDossierNumber(ValidationException &$ve,
//                                               IString $dossier) {
//    if (preg_match("#^[0-9]{14}$#", $dossier->toString()) == 0) {
//      $ve->addErrorMessage(self::DOSSIER,
//                           "The dossier number is not in a correct format.");
//    }
//  }

  /**
   * Validates a phone number.
   * @param ValidationException $ve The exception to update.
   * @param IString $phone The phone.
   * @link http://be2.php.net/manual/en/function.preg-match.php
   */
  public static function validateBePhoneNumber(ValidationException &$ve,
                                               String $phone, String $field) {
    if (preg_match("#^[0-9]{9,10}$#",
                   $phone->replace(new String("+32"), new String("0"))->toString()) == 0) {
      $ve->addErrorMessage($field, new String("[phone-format]"));
    }
  }

  /**
   * Validates a belgian phone number.
   * @param ValidationException $ve The exception to update.
   * @param IString $phone The phone.
   * @link http://be2.php.net/manual/en/function.preg-match.php
   */
  public static function validateLuxGsmNumber(ValidationException &$ve,
                                              String $gsm, String $field) {
    if (preg_match("#^\+352((79|2679)[0-9]{4}|4[0-9]{5})$#", $gsm->toString()) == 0) {
      $ve->addErrorMessage($field, new String("[gsm-format]"));
    }
  }

  /**
   * Validates a luxemburg phone number.
   * @param ValidationException $ve The exception to update.
   * @param IString $phone The phone.
   * @link http://be2.php.net/manual/en/function.preg-match.php
   */
  public static function validateBeGsmNumber(ValidationException &$ve,
                                             String $gsm, String $field) {

    if (preg_match("#^\+?(32|0)4[7-9][0-9]{7}$#", $gsm->toString()) == 0) {
      $ve->addErrorMessage($field, new String("[phone-format]"));
    }
  }

  /**
   * Validates a date like DD/MM/YYYY, MM/DD/YYYY, DD/MM/YY or MM/DD/YY.
   * @param ValidationException $ve The exception to update.
   * @param IString $date The date.
   * @link http://be2.php.net/manual/en/function.preg-match.php
   */
  public static function validateBeDate(ValidationException &$ve, String $date,
                                        String $field) {
    if (preg_match("#^((0?[1-9]|[12][0-9]|3[01])[- /.](0?[1-9]|1[012])|(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01]))[- /.](19|20)?\d\d$#",
                   $date->toString()) == 0) {
      $ve->addErrorMessage($field, new String("[date-format]"));
    }
  }

  /**
   * Validates an iban account number.
   * @param ValidationException $ve The exception to update.
   * @param String $iban The iban account number.
   */
  public static function validateIban(ValidationException &$ve, String $iban,
                                      String $field) {
    if (preg_match("#^[a-zA-Z]{2}[0-9]{2}[a-zA-Z0-9]{4}[0-9]{7}([a-zA-Z0-9]?){0,16}$#",
                   $iban->toString()) == 0) {
      $ve->addErrorMessage($field, new String("[field-wrong]"));
    }
  }

  /**
   * Validates a bic number.
   * @param ValidationException $ve The exception to update.
   * @param IString $bic The bic.
   */
//  public static function validateBic(ValidationException &$ve, IString $bic) {
//    if (preg_match("#^[a-zA-Z]{6}[a-zA-Z0-9]{2,5}$#", $bic->toString()) == 0) {
//      $ve->addErrorMessage(self::BIC, "The bic is not in a correct format.");
//    }
//  }

  /**
   * Validates a belgian zip code.
   * @param ValidationException $ve The exception to update.
   * @param Integer $zip The zip code.
   */
  public static function validateBeZipCode(ValidationException &$ve,
                                           Integer $zip, String $field) {
    if ($zip->integerValue() < 1000 || $zip->integerValue() > 9999) {
      $ve->addErrorMessage($field, new String("[zipOutOfRange]"));
    }
  }

}

?>
